<?php
/**
 * 	合同管理
 *  author： chengda
 *  date  ： 2017-11-22
 */
namespace Admin\Controller;

class ContractController extends AbstractController  {

	protected $Form;

	public function _initialize(){
		$this->Form=D('Contract');
    }
	

    public function index(){

	    $res = $this->Form->MyshowList();
		$this->assign("list",$res['list']);
    	$this->assign("page",$res['page']);
        $this->display();
    }


    public function save($type = false, $id = 0){
		$data['selectlist']['InOutType']=$this->Form->InOutType();
		$data['selectlist']['ContractType']=$this->Form->ContractType();
		$data['selectlist']['ContractLevel']=$this->Form->ContractLevel();
		$this->assign("data", $data);
		$this->display();
    }
	
    function add(){
		$ContractPartner=D('contractpartner');
		//开始事物
		$this->Form->startTrans(); 
		$CNO=$this->Form->getNowNO();
		$_POST['CNO']='C'.$CNO;
		$_POST['AddTime']=date('y-m-d h:i:s',time());
		$ContractPartnerList=$_POST['ContractPartnerList'];

		if(!$this->Form->create()){
			exit($this->error($this->Form->getError()));
		}else{
			$result = $this->Form->add();
	        if($_POST['ContractPartnerList']){
				foreach ($ContractPartnerList as $key => $value) {
					$Contractdata[]=json_decode($value,true);
				}
				foreach ($Contractdata as $key => $value) {
					$value['ContractID']=$result;
					$value['addTime']=$_POST['AddTime'];
					$addiddata[]=$value;
				}
				$result2=$ContractPartner->addAll($addiddata);
			}else{
				$result2=1;
			}
			if($result&&$result2){
				$this->Form->commit();
				//传合同ID
				$datajump['ID']=$result;
				$datajump['ContractType']=I('InOutType');
				$this->success($datajump,'添加成功！','1');
			}else{
				$this->Form->rollback();
				$this->error('添加错误！');
			}
		}
    }

    function delete(){
    	$res = $this->Form->MydeletP();
    	//var_dump($res); exit();
    	if($res['state']){
			$this->success($res['msg']);
		}else{
			$this->error($res['msg']);
		}
    }


    function edit(){
		if(I('ID'))
		{
			$res = $this->Form->Myshow((int)I('ID'));
			$this->assign("editdata",$res);
			$data['selectlist']['InOutType']=$this->Form->InOutType();
			$data['selectlist']['ContractType']=$this->Form->ContractType();
			$data['selectlist']['ContractLevel']=$this->Form->ContractLevel();
			$this->assign("data", $data);
			$this->display("save");
		}else{
			$this->error('参数错误');
		}
    }


    function update(){
		if(I('ID'))
		{
			$result = $this->Form->Myupdata();
			if($result['status'] == 1 && !empty($result)){
				//$this->success($result['msg']);
				$datajump['ID']=I('ID');
				$datajump['ContractType']=I('InOutType');
				$this->success($datajump,'添加成功！','1');
			}else if($result['status'] == 0 && !empty($result)){
				$this->error($result['msg']);
			}
		}
    }

	// 选择合作伙伴
	public function ContractPartner()
	{
		$data['selectlist']['PartnerType']=$this->Form->ContractPartner();
		$this->assign("data",$data);
		$this->display();
	}

	//合同详情
	public function ContractBody($type='',$ContractID=''){
		$ContractBody=D('contractbody');
		if($type=='add'){
			// 添加合同主题
			// 如何查询到constractID则更新
			$CID=$ContractBody->where(array('ContractID' => $ContractID))->find();
			//var_dump($_POST); exit();
			if($CID){
				$ContractBody->create();
				
				$result = $ContractBody->where(array('ContractID' => $ContractID))->save();
				if($result){
					$AddTime=date('Y-m-d H:i:s',time());
					$this->addfj($_POST['ImgInfo'],$ContractID,$AddTime);
					//exit();
					$this->success('添加成功！');
				}else{
					$this->error('添加错误！');
				}
			}
			if(!$ContractBody->create()){
				exit($this->error($ContractBody->getError()));
			}else{
				$result = $ContractBody->add();
			if($result){
				//传合同ID
				$this->success('添加成功！');
			}else{
				$this->error('添加错误！');
			}
			}

		}
		elseif ($type=='delete') {
			
		}
		elseif($type==''&$ContractID!=''){
			$this->assign("ContractID",$ContractID);
			$sql="SELECT
			A.ContractID,
			A.Content,
			A.CTID,
			A.StampID,
			A.WMID,
			B.`Name` AS StampID_Name,
			C.`Name` AS WMID_Name,
			D.Title  AS CTID_NAME
			FROM
			contractbody AS A
			LEFT JOIN stamp AS B ON (
			A.StampID = B.ID
			)
			LEFT JOIN watermark AS C ON (
			A.WMID = C.ID
			)
			LEFT JOIN contracttemplate AS D ON(
			A.CTID=D.ID
			) WHERE A.ContractID=".$ContractID."";
			
			$ContractAttachment=D('Contractattachment');
			$cam=$ContractAttachment->field('ContractID,Name,SavePath')->where('ContractID='.$ContractID)->select();
			//var_dump($cam);
			$data=$ContractBody->query($sql);
			$data[0]['cam']=$cam;
			//var_dump($cam);
			$this->assign("data",$data[0]);
			$this->display();
		}
		else{
			
			$this->error('提交异常');
		}
		
	}


	function addfj($ImgInfo,$ContractID,$AddTime){
		//var_dump($ImgInfo);//exit();
		$ContractAttachment=D('Contractattachment');
		$ContractAttachment->where('ContractID='.$ContractID)->delete();
		foreach ($ImgInfo as $key => $value) {
			$value=json_decode($value,true);
			$value['ContractID']=$ContractID;
			$value['AddTime']=$AddTime;
			$arr[]=$value;
		}
		//var_dump($arr); exit();
		$ContractAttachment->create($arr);
		$ContractAttachment->addAll($arr);

	}
	//获取组织列表
    public function Organizationselect(){
		$this->assign('list',$this->Organizationshow(0));
        $this->display();
    }


    public function Customerselect(){
    	$list=$this->Form->Customershow();
    	//var_dump($aa['list']);
    	$this->assign('list',$list['list']);
        $this->display();
    }
	/**
	 返回部门列表数据
	 */
	public function Organizationshow($pid = -1){
		if($pid == -1){
			$in = I();
			$parentID = $in['ParentID'];
		}else{
			$parentID = $pid;
		}
		$sql = "SELECT *,(SELECT count(id) FROM department WHERE ParentID = a.ID AND IsDel = 0) AS coutsonID FROM department AS a WHERE ParentID = {$parentID} AND IsDel = 0";
		$res = $this->Form->query($sql);
		if($pid == -1){
			$this->ajaxReturn($res);
		}else{
			return $res;
		}
    }


    function pdfview($ContractID){
    	$this->Form->PDF($ContractID);
    }

    function view($ContractID=0){

    	$ContractBody=D('contractbody');
		$ContractBody=$ContractBody->where(array('ContractID' => $ContractID))->find();
		$this->assign("Content",$ContractBody['Content']);
		$this->display();
    }
 
    function docview($ContractID=0){
    	$ContractBody=D('contractbody');
		$ContractBody=$ContractBody->where(array('ContractID' => $ContractID))->find();
		$this->assign("Content",$ContractBody['Content']);
		$this->display();
    }

    /**
     * 上传图片方法
     */
    function markupload(){

    	if($_FILES){
    		$rtn = "";
    		$config = array(
    			'maxSize' => 5*1024*1024,
    			'rootPath' => './data/upload/Contract/contractattachment/',
    			'savePath' => '',
    			'saveName' => array('uniqid',''),
    			'exts' => array('rar','zip','doc','xls','docx','xlsx','pdf'),
    			'autoSub' => true,
    			'subName' => array('date','Ymd')
    		);
    		$this->mkdirs($config['rootPath']);
	    	$upload = new \Think\Upload($config);// 实例化上传类
	    	// 上传文件
	    	$info = $upload->upload($_FILES);
	    	if(!$info) {// 上传错误提示错误信息
	    		$rtn = $this->error($upload->getError());
	    	}else{// 上传成功
	    		foreach ($info as $key=>$img){
	    			if($key==0){
	    				$rtn .= $config['rootPath'].$img['savepath'].$img['savename'];
	    			}else{
	    				$rtn .= "|".$config['rootPath'].$img['savepath'].$img['savename'];
	    			}
	    		}
	    	}
	    	echo $rtn;
    	}

    }


    public function select(){
    	$res = $this->Form->MyshowList();
		$this->assign("list",$res['list']);
    	$this->assign("page",$res['page']);
    	$this->display();
    }

}